package com.jsaas.model.base;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.TableMapping;
import com.jsaas.core.qparams.PageQueryParams;
import com.jsaas.core.qparams.QueryParamsUtils;
import com.jsaas.model.Menu;
import com.jsaas.utils.MyUtils;
import com.jsaas.utils.ShiroUtils;

/**   
* @Title: BaseModel.java 
* @Package com.jsaas.model.base 
* @Description: TODO(用一句话描述该文件做什么) 
* @author tuozq 
* @date 2017年10月27日 下午12:00:49 
* @version V1.0   
*/
@SuppressWarnings({ "rawtypes", "serial" })
public class BaseModel<M extends BaseModel> extends Model<M> {
	
	@Override
	public boolean save() {
		// TODO Auto-generated method stub
		Table table = getTable();
		if(table.hasColumnLabel("id") && StrKit.notBlank(this.getStr("id"))){
			return this.update();
		}
		if(table.hasColumnLabel("id")){
			this.set("id", MyUtils.newId());
		}
		if(table.hasColumnLabel("createuserid")){
			this.set("createuserid", ShiroUtils.getCurUser().getUserId());
		}
		if(table.hasColumnLabel("createtime")){
			this.set("createtime", new Date());
		}
		return super.save();
		
	}
	
	@Override
	public boolean update() {
		// TODO Auto-generated method stub
		Table table = getTable();
		if(table.hasColumnLabel("updateuserid")){
			this.set("updateuserid", ShiroUtils.getCurUser().getUserId());
		}
		if(table.hasColumnLabel("updatetime")){
			this.set("updatetime", new Date());
		}
		return super.update();
	}
	
	/**
	 * 保存树形结构的model
	 * @return
	 */
	public boolean saveTree(){
		boolean result = false;
		String LONGNO = "long_no";
		String LONGNAME = "long_name";
		String NAME = "menu_name";
		String NO = "menu_no";
		String separator = "-";
		String table = getTable().getName();
		//是否叶子节点
		boolean isLeaf = this.getBoolean("isLeaf");
		String parentId = this.getStr("parent_id");
		Model parent = this.findFirst("select * from "+ table + " where id = ? ", parentId);
		Model old = this.findFirst("select * from "+ table + " where id = ? ", this.getStr("id"));
		Model oldparent = old == null ? parent : this.findFirst("select * from "+ table + " where id = ? ", old.getStr("parent_id"));
		if(!isLeaf){
			//更新所有子节点的长编码和长名称
			String new_nostr = parent.getStr(LONGNO) + this.getStr(NO) + separator;//修改后的长编码
			String old_nostr = oldparent.getStr(LONGNO) + old.getStr(NO) + separator;//修改前的长编码
			String new_namestr = parent.get(LONGNAME) + this.getStr(NAME) + separator;
			String old_namestr = oldparent.get(LONGNAME) + old.getStr(NAME) + separator;
			//当前节点的子节点
			List<M> childrens = this.find("select * from "+ table + " where long_no like '"+ old_nostr +"%' ");
			for (M m : childrens) {
				m.set(LONGNO, m.getStr(LONGNO).replace(old_nostr, new_nostr));
				m.set(LONGNAME, m.getStr(LONGNAME).replace(old_namestr, new_namestr));
				m.update();
			}
			
		}
		//修改了父节点
		if(!parent.get("id").equals(oldparent.get("id"))){
			List<M> childrens = this.find("select * from "+ table + " where parent_id = ? ", old.getStr("parent_id"));
			//如果原菜单的父菜单剩下最后一子菜单时  更新原父菜单为叶子节点
			if(childrens.size() == 1){
				oldparent.set("isLeaf", true);
				oldparent.update();
			}
		}
		
		if(parent.getBoolean("isLeaf")){
			parent.set("isLeaf", false);
			parent.update();
		}
		
		//更新当前节点的长编码和长名称
		this.set(LONGNO, parent.get(LONGNO) + this.getStr(NO) + separator);
		this.set(LONGNAME, parent.get(LONGNAME) + this.getStr(NAME) + separator);
		this.save();
		
		result = true;
		return result;
	}
	
	/**
	 * 校验重复数据 
	 * @param filed 字段
	 * @param value 字段值
	 * @return
	 */
	public boolean isExist(String filed, String value){
		String table = getTable().getName();
		List<Object> params = new ArrayList<Object>();
		String id = this.getStr("id");
		String sql = " select id from  " + table + " where  " + filed + " = ? ";
		params.add(value);
		if(StrKit.notBlank(id)){
			sql += " and id != ? ";
			params.add(id);
		}
		return this.findFirst(sql, params.toArray()) == null ? false : true;
	}
	
	public Page<M> pageinit(PageQueryParams params){
		return paginate(params.getPage(), params.getSize(), params.getSqlpara());
	}
	
	public Table getTable(){
		return TableMapping.me().getTable(this.getClass());
	}
}
